package com.maila.xiaoaoschool.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.activity.NoticeActivity;
import com.maila.xiaoaoschool.activity.RecommendActivity;
import com.maila.xiaoaoschool.activity.SaleActivity;
import com.maila.xiaoaoschool.activity.ScoreActivity;
import com.maila.xiaoaoschool.activity.ShopActivity;

import com.maila.xiaoaoschool.activity.XiaoAoMallActivity;
import com.maila.xiaoaoschool.adapter.MainUAdapter;
import com.maila.xiaoaoschool.api.FindApi;
import com.maila.xiaoaoschool.api.IndexApi;
import com.maila.xiaoaoschool.entity.IndexRecommendShop;
import com.maila.xiaoaoschool.entity.News;
import com.maila.xiaoaoschool.entity.XiaoArt;
import com.maila.xiaoaoschool.net.VolleyInterface;
import com.maila.xiaoaoschool.util.Constants;
import com.maila.xiaoaoschool.util.NullPointUtils;
import com.maila.xiaoaoschool.util.StartActivityUtils;
import com.maila.xiaoaoschool.widget.NoScrollGridView;
import com.maila.xiaoaoschool.widget.UPMarqueeView;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by mark on 2017/9/23.
 */

public class FindFragment extends Fragment {
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.ll_search)
    AutoLinearLayout llSearch;
    @BindView(R.id.toolbar)
    AutoRelativeLayout toolbar;
    @BindView(R.id.btn_sell)
    ImageView btnSell;
    @BindView(R.id.btn_mall)
    ImageView btnMall;
    @BindView(R.id.btn_message)
    ImageView btnMessage;
    @BindView(R.id.btn_jifen)
    ImageView btnJifen;
    @BindView(R.id.btn_tuijian)
    ImageView btnTuijian;
    @BindView(R.id.iv_ad)
    ImageView ivAd;
    @BindView(R.id.laba)
    ImageView laba;
    @BindView(R.id.upview1)
    UPMarqueeView upview1;
    @BindView(R.id.gv_youxuan)
    NoScrollGridView gvYouxuan;
    @BindView(R.id.iv_art)
    ImageView ivArt;
    @BindView(R.id.tv_art_content)
    TextView tvArtContent;
    Unbinder unbinder;
    MainUAdapter mainUAdapter;
    Gson gson = new Gson();
    String lat = "36.71287";
    String lng = "119.126626";
    String adcode = "1";
    int page = 1;
    int size = 6;

    List<View> views = new ArrayList<>(); //新闻滚动布局集
    @BindView(R.id.ll_promotion_header)
    AutoLinearLayout llPromotionHeader;

    public static FindFragment newInstance(String param1) {
        FindFragment fragment = new FindFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, container, false);
        AutoUtils.autoSize(view);
        unbinder = ButterKnife.bind(this, view);
        mainUAdapter = new MainUAdapter(getActivity());
        gvYouxuan.setAdapter(mainUAdapter);
        gvYouxuan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                StartActivityUtils.startToShop(getActivity(), ShopActivity.class,mainUAdapter.getMdata().get(i).getId(),mainUAdapter.getMdata().get(i).getShopname(),mainUAdapter.getMdata().get(i).getEvaluate());
            }
        });
        initdata();
        return view;
    }

    /**
     * 初始化界面数据
     */
    private void initdata() {
        initartdata();
        initpromotiondata();
        initnews();
    }

    /**
     * 初始化轮播新闻
     */
    private void initnews() {
        new FindApi(getActivity(), new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                try{
                    News news=gson.fromJson(result,News.class);
                    if(news.getResult()==1){
                        initviews(news.getData());
                    }

                }catch (Exception e){

                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).get_news_list();
    }

    private void initviews(final List<News.DataBean> data) {
        for (int i = 0; i < data.size(); i++) {
            //设置滚动的单个布局
            LinearLayout moreView = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.item_view, null);
            //初始化布局的控件
            TextView tv1 = (TextView) moreView.findViewById(R.id.tv1);

            final int finalI = i;
            moreView.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    Toast.makeText(context, position + "你点击了" + data.get(position).toString(), Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent();//创建Intent对象
                    intent.setAction(Intent.ACTION_VIEW);//为Intent设置动作
                    intent.setData(Uri.parse(data.get(finalI).getUrl()));//为Intent设置数据
                    startActivity(intent);
                }
            });
            //进行对控件赋值
            if(!NullPointUtils.checknull(data.get(i).getTitle())){
                tv1.setText(data.get(i).getTitle());
            }

            //添加到循环滚动数组里面去
            views.add(moreView);
        }
        upview1.setViews(views);
    }

    /**
     *
     */
    private void initpromotiondata() {

        new IndexApi(getActivity(), new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                IndexRecommendShop indexRecommendShop = gson.fromJson(result, IndexRecommendShop.class);
                if (indexRecommendShop.getResult() == 1) {
                    llPromotionHeader.setVisibility(View.VISIBLE);
                    mainUAdapter.setMdata(indexRecommendShop.getData());
                } else {
                    llPromotionHeader.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).getPormotionShop(lat, lng, page, size, adcode);
    }

    /**
     * 初始化每日一文
     */
    private void initartdata() {
        new FindApi(getActivity(), new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                XiaoArt xiaoArt = gson.fromJson(result, XiaoArt.class);
                if (xiaoArt.getResult() == 1) {
                    Glide.with(getActivity()).load(Uri.parse(Constants.URL_ROOT_XIAOAO + xiaoArt.getData().get(0).getImg())).into(ivArt);
                    tvArtContent.setText(xiaoArt.getData().get(0).getTitle() + "-" + xiaoArt.getData().get(0).getContent());
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).getXiaoArt();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ll_search, R.id.btn_sell, R.id.btn_mall, R.id.btn_message, R.id.btn_jifen, R.id.btn_tuijian, R.id.upview1, R.id.iv_art})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_search:
                break;
            case R.id.btn_sell:
                StartActivityUtils.start(getActivity(), SaleActivity.class);
                break;
            case R.id.btn_mall:
                StartActivityUtils.start(getActivity(), XiaoAoMallActivity.class);
                break;
            case R.id.btn_message:
                StartActivityUtils.start(getActivity(), NoticeActivity.class);
                break;
            case R.id.btn_jifen:
                StartActivityUtils.start(getActivity(), ScoreActivity.class);
                break;
            case R.id.btn_tuijian:
                StartActivityUtils.start(getActivity(), RecommendActivity.class);

                break;
            case R.id.upview1:
                break;
            case R.id.iv_art:
                break;
        }
    }
}
